package com.cl.code.generate.source.database.entity;

import com.cl.code.common.utils.CamelCaseUtils;
import lombok.Getter;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 字段信息封装类
 *
 * @author chengliang
 * @since 1.0.0
 */
@Getter
public class TableFieldMetadata {
    private final String jdbcType;
    private final String javaType;
    private final String comment;
    private final String fieldName;
    private final String columnName;

    public TableFieldMetadata(ResultSet column) throws SQLException {
        this.columnName = column.getString("COLUMN_NAME");
        // 将字段名转换为驼峰命名（首字母小写）
        this.fieldName = CamelCaseUtils.toLowerCamelCase(columnName);
        this.jdbcType = column.getString("TYPE_NAME");
        this.javaType = jdbcTypeToJavaType(jdbcType);
        this.comment = column.getString("REMARKS");
    }

    private String jdbcTypeToJavaType(String jdbcType) {
        return switch (jdbcType.toUpperCase()) {
            case "VARCHAR", "JSON", "TEXT" -> "String";
            case "INT", "TINYINT" -> "Integer";
            case "BIGINT" -> "Long";
            case "DECIMAL" -> "BigDecimal";
            case "BIT" -> "Boolean";
            default -> "Object";
        };
    }

}